<?php

class Application_Form_Edit extends Zend_Form
{

    public function init()
    {
        /* Form Elements & Other Definitions Here ... */

        //array cities
        $cites = array(
            'Hà Nội' => "Hà Nội",
        );
        //array hanoi districts
        $hanoiDistricts = Zend_Registry::get('hanoiDistricts');

        //array hanoi schools
        $hanoiSchools = Zend_Registry::get('hanoiSchools');
        //all classes
        $classes = Zend_Registry::get('classes');
        //all student years
        $years = Zend_Registry::get('years');

        /* Form Elements & Other Definitions Here ... */
        $this->setDisableLoadDefaultDecorators(true);

        $this->setMethod('post');
        $this->setAction('');

        $username = $this->createElement('text', 'username');
        $username->setRequired(true);
        $username->addValidator('NotEmpty', true);
        $username->addErrorMessage('Thiếu họ và tên');
        $username->removeDecorator('Label');
        $username->removeDecorator('HtmlTag');
        $username->removeDecorator('Errors');


        $password = $this->createElement('password', 'password');
//        $password->setRequired(true);
        $password->removeDecorator('Label');
        $password->removeDecorator('HtmlTag');
//        $password->addValidator('NotEmpty', true);
//        $password->getValidator('NotEmpty')->setMessage('Thiếu mật khẩu');
        $password->removeDecorator('Errors');
        $password->addValidator('stringLength', true, array(6,20));
        $password->getValidator('stringLength')->setMessage('Mật khẩu phải dài hơn 6 kí tự');


        $confirmPassword = $this->createElement('password', 'confirmpassword');
//        $confirmPassword->setRequired(true);
        $confirmPassword->removeDecorator('Label');
        $confirmPassword->removeDecorator('HtmlTag');
//        $confirmPassword->addValidator('NotEmpty', true);
//        $confirmPassword->addErrorMessage('Thiếu xác nhận mật khẩu');
        $confirmPassword->removeDecorator('Errors');

        $email = new Zend_Form_Element_Text('email');
        $email->setRequired(true);
        $email->addValidator('NotEmpty',true);
        $email->getValidator('NotEmpty')->setMessage('Thiếu email');
        $email->removeDecorator('Label');
        $email->removeDecorator('HtmlTag');
        $email->removeDecorator('Errors');
        $email->addValidator('EmailAddress', true);
        $email->addValidator('Db_NoRecordExists', true, array('table' => 'students', 'field' => 'email'));
        $email->getValidator('Db_NoRecordExists')->setMessage('Email này đã được đăng ký');

        $friendEmail = new Zend_Form_Element_Text('friendemail');
        $friendEmail->removeDecorator('Label');
        $friendEmail->removeDecorator('HtmlTag');
        $friendEmail->removeDecorator('Errors');


        $phoneNumber = new Zend_Form_Element_Text('phonenumber');
        $phoneNumber->setRequired(true);
        $phoneNumber->addValidator('NotEmpty',true);
        $phoneNumber->getValidator('NotEmpty')->setMessage('Thiếu số điện thoại');
        $phoneNumber->removeDecorator('Label');
        $phoneNumber->removeDecorator('HtmlTag');
        $phoneNumber->removeDecorator('Errors');
        $phoneNumber->addValidator('Db_NoRecordExists', true, array('table' => 'students', 'field' => 'phonenumber'));
        $phoneNumber->getValidator('Db_NoRecordExists')->setMessage('Số điện thoại này đã được đăng ký');
        $phoneNumber->addValidator('stringLength',true,array(10,11));
        $phoneNumber->getValidator('stringLength')->setMessage('Số điện thoại không hợp lệ');


        $city = new Zend_Form_Element_Select('city');
        $city->setMultiOptions($cites);
        $city->removeDecorator('Label');
        $city->removeDecorator('HtmlTag');

        $district = new Zend_Form_Element_Select('district');
        $district->setMultiOptions($hanoiDistricts);
        $district->removeDecorator('Label');
        $district->removeDecorator('HtmlTag');

        $school = new Zend_Form_Element_Select('school');
        $school->removeDecorator('Label');
        $school->removeDecorator('HtmlTag');
        $school->setAttribs(array('style' => 'width: 150px;'));
        $school->setRequired(true);
        $school->setMultiOptions($hanoiSchools);


        $year = new Zend_Form_Element_Select('year');
        $year->setMultiOptions($years);
        $year->removeDecorator('Label');
        $year->removeDecorator('HtmlTag');

        $gender = new Zend_Form_Element_Radio('gender');
        $gender->setRequired(true);
        $gender->setSeparator(' ');
        $gender->setMultiOptions(array(
                    'Nam' => "Nam",
                    'Nữ' => 'Nữ'
                ));
       $gender->removeDecorator('Label');
       $gender->removeDecorator('HtmlTag');
       $gender->removeDecorator('Errors');
       $gender->addValidator('NotEmpty',true);
       $gender->getValidator('NotEmpty')->setMessage('Thiếu giới tính');

        $fromClass = new Zend_Form_Element_Select('fromclass');
        $fromClass->setMultiOptions($classes);
        $fromClass->removeDecorator('Label');
        $fromClass->removeDecorator('HtmlTag');

        $toClass = new Zend_Form_Element_Select('toclass');
        $toClass->setMultiOptions($classes);
        $toClass->removeDecorator('Label');
        $toClass->removeDecorator('HtmlTag');


        $subjects = $this->createElement('text', 'subjects');
        $subjects->setRequired(true);
        $subjects->removeDecorator('Label');
        $subjects->removeDecorator('HtmlTag');
        $subjects->removeDecorator('Errors');
        $subjects->addValidator('NotEmpty',true);
        $subjects->getValidator('NotEmpty')->setMessage('Thiếu môn dạy');
        $subjects->addValidator('stringLength',true, array(1,20));
        $subjects->getValidator('stringLength')->setMessage('Độ dài môn dạy phải nhỏ hơn 20 kí tự');

        $teachTime = $this->createElement('text', 'teachtime');
        $teachTime->removeDecorator('Label');
        $teachTime->removeDecorator('HtmlTag');
        $teachTime->addValidator('stringLength',true, array(0,30));
        $teachTime->getValidator('stringLength')->setMessage('Độ dài thời gian dạy phải nhỏ hơn 30 kí tự');
        $teachTime->removeDecorator('Errors');

        $salary = $this->createElement('text', 'salary');
        $salary->removeDecorator('Label');
        $salary->removeDecorator('HtmlTag');
        $salary->addValidator('stringLength',true, array(0,20));
        $salary->getValidator('stringLength')->setMessage('Độ dài mức lương phải nhỏ hơn 20 kí tự');
        $salary->removeDecorator('Errors');

        $description = $this->createElement('textarea', 'description');
        $description->removeDecorator('Label');
        $description->setAttrib('ROWS', 7);

        $award = $this->createElement('textarea', 'award');
        $award->removeDecorator('Label');
        $award->setAttrib('ROWS', 7);

        $this->setAttrib('enctype', 'multipart/form-data');
        $avatar = new Zend_Form_Element_File('avatar');
        $avatar->setDestination(APPLICATION_PATH.'/../public/img/avatars');
        $avatar->addValidator(new Zend_Validate_File_FilesSize(array('min' => 1, 'max' => 10000000,'bytestring'=>true)));
        $avatar->addValidator(new Zend_Validate_File_Extension(array('gif,png,jpg')));

        $update = $this->createElement('submit', 'update');
        $update->setLabel('Cập nhật')
                ->setIgnore(true);

        $this->setDecorators(array(array('ViewScript',array('viewScript' => 'form/_edit.phtml')),'Form'));

        $this->addElements(array(   $username,
                                    $password,
                                    $confirmPassword,
                                    $email,
//                                    $friendEmail,
                                    $phoneNumber,
                                    $gender,
                                    $city,
                                    $district,
                                    $school,
                                    $year,
                                    $fromClass,
                                    $toClass,
                                    $subjects,
                                    $teachTime,
                                    $salary,
                                    $description,
                                    $award,
                                    $update));


    }

}

